<?php defined('ByShopWWI') or exit('Access Invalid!');?>

<div class="tabmenu">
    <?php include template('layout/submenu');?>
</div>
<div class="ncsc-form-default">

    <form method="post" action="<?php echo urlShop('store_order','handle'); ?>" id="callcenter_form" class="ncs-message">
        <input type="hidden" name="form_submit" value="ok" />
		
		
		<!--新代码-->
			
		
		<table class="ncsc-default-table bd-line order_table">
		  <thead>
			<tr>
				<th>商品选择</th>
				<!--<th>托管</th>-->
				<th>数量</th>
				<th>小计</th>
				<th>单位</th>
				<th>删除</th>
			</tr>
		  </thead>
			  <tbody>
				
			  </tbody>
		  <tfoot>
			</tfoot>
		</table>
		<div style="text-align:right;display:inline-block;width: 100%; ">
			
			<a href="javascript:void(0);" class="ncbtn ncbtn-aqua mt10 line_add">新增一行</a>
		</div>
		<div style="text-align:right;display:inline-block;width: 100%; ">
		
		<table class="ncsc-default-table bd-line">
				<tr>
						<td>买家名称</td>
						<td>电话号码</td>
						<!--<td>发货状态</td>-->
						<td>录单日期</td>
						<td>总计</td>
						<td>单位</td>
				</tr>
				<tr>
						<td>
								<input type="text" class="text" id="buyer" name="buyer_name" value="">
						</td>
						<td>
								<input type="text" class="text" name="buyer_phone" value="">
						</td>
						<!--<td>
								<select name="order_state">
										<option value="1">已发货</option>
										<option value="0">未发货</option>
								</select>
						</td>-->
						<td><input type="text" id="order_date" class="text order_date" name="order_date" value=""></td>
						<td><input type="text" class="text order_total tr" name="order_total" value="0.00" readonly="readonly"></td>
						<td>元</td>
				</tr>
		</table>
		</div>
		<div style="text-align:right;display:inline-block;width: 100%;margin-top:10px;">
			
			<label class="submit-border"><input type="button" class="submit getcount" value="结算" /></label>
			<label class="submit-border"><input type="submit" class="submit order_add" style="background:gray;" value="添加订单" disabled="true" /></label>
		</div>
		
		
		<textarea class="pre_table" style="display: none;">
				<tr>
					<td>
					<select class="input goods_id" data-name="goods_id">
							<option value="null">请选择</option><!--这行不要删，下面保持数据格式，计算程序才能运行-->
							<?php foreach ($output['goods'] as $value ) { ?>
								<option value="<?php echo $value['goods_id'] ?>" data-price="<?php echo $value['goods_price'] ?>">
								<?php echo $value['goods_name'] ?> / <?php echo $value['goods_price'] ?>
								</option>
						   <?php } ?>
					</select>
					</td>
					<!--
					<td>
					<select class="input" data-name="tuoguan">
							<option value="0">否</option>
							<option value="1">是</option>
					</select>
					</td>
					-->
					<td><input type="text" class="text w30 input count" data-name="count" value="1"></td>
					<td><input type="text" class="text w80 input subtotal tr" data-name="price" value="0.00" readonly="readonly"></td>
					<td>元</td>
					<td><a href="javascript:void(0);" class="ncbtn mt10 line_del">删除</a></td>
				</tr>
		</textarea>
		
    </form>
	
	
</div>

<script src='<?php echo SHOP_RESOURCE_SITE_URL; ?>/js/laydate/laydate.js'></script>

<script>

$(function(){


$(".order_table>tbody").append($(".pre_table").val())
init()

laydate({
   elem: '#order_date'
})


$(".line_add").click(function(){
	$(".order_table>tbody").append($(".pre_table").val())
	init()
})


$(".getcount").click(function(){
	match_count()
})
function init(){
	$(".line_del").unbind()
	$(".line_del").click(function(){
		if($(".order_table>tbody>tr").length>1){
			$(this).parent().parent().remove()
		}
	})
	
	$(".order_table tr").each(function(){
		index=$(this).index()
		$(this).find(".input").each(function(){
				$(this).attr("name","order_list["+index+"]["+$(this).data("name")+"]")
		})
	})
}

function match_count(){
	
	
	
	total = 0
	dis_select = 0;
	dis_count = 0;
	error_msg =""
	$(".order_table tbody tr").each(function(){
		
		$(this).find(".goods_id").removeClass("alert-error")
		
		p = $(this).find(".goods_id option:selected").data("price")*1

		i = $(this).find(".goods_id option:selected").val()

		if(i=="null"){
			dis_select +=1
			$(this).find(".goods_id").addClass("alert-error")
			
		}

		n = $(this).find(".count").val()
		//console.log(n)
		
		if(n==0){
			dis_count +=1
			$(this).find(".count").addClass("alert-error")
		}
		
		if(isNaN(n)){
			dis_count +=1
			$(this).find(".count").addClass("alert-error")
		}
		
		n = n*1
		c = p*n
		$(this).find(".subtotal").val(c.toFixed(2))
		
		total = total+c
		
		
	})
	//console.log(dis_select)
	var pass = true
	
	
	if(dis_count>0){
		error_msg += "<p>商品数量设置错误。</p>"
		pass =false
	}
	if(dis_select>0){
		error_msg += "<p>未选择商品。</p>"
		pass =false
	}
	
	if($("input[name=buyer_name]").val().length<1){
		error_msg += "<p>未填写'买家名称'。</p>"
		pass =false
	}
	
	
	
	if($("input[name=buyer_phone]").val().length<1){
		error_msg += "<p>未填写'电话号码'。</p>"
		pass =false
	}
	if($("input[name=order_date]").val().length<1){
		error_msg += "<p>未填写'录单日期'。</p>"
		pass =false
	}
	
	if(pass){
		$(".order_total").val(total.toFixed(2))
		true_submit()
	}else{
		showDialog(error_msg, 'alert', '错误信息', '', '', '', '', '', '', 3);
	}
	
	
	
}

function true_submit(){
	$(".order_add").attr("disabled",false)
	$(".order_add").attr("style","")
}
 
})
/*
    $(function(){

        var check1=false;
        submit_check(false)
        $("input[name=phone]").bind('input propertychange',function(){
            get_p()
        })
        $("select[name=goods_name_show]").bind('change',function(){
            get_p()
        })

        $("input[name=count]").bind('input propertychange',function(){
            get_p()
        })

        $("input[name=count]").change(function(){
            if($("input[name=count]").val().length<1){
                $("input[name=count]").val(1)
                get_p()
            }
        })

        function get_p(){

            var p = $("select[name=goods_name_show]").find("option:selected")
            var n = $("input[name=count]").val()
            if(n.length>0){
                if (n!=null && n!="" && isNaN(n)){
                    var n = 1
                    $("input[name=count]").val(n)
                }

                if(p.index()!=0){
                    p = p.text().split("/")
                    var k = p
                    p = p[1].match(/[0-9].*.[0-9]/g);
                    $("input[name=goods_price]").val(p)

                    $("input[name=price]").val(toDecimal2(p*n))

                    $("input[name=goods_name]").val(k[0])


                }
            }
            var phone = $("input[name=phone]").val()

            if(phone.length>0){
                if(checkPhone(phone)){
                    check1=true;
                }else{
                    check1=false;
                }
            }

            var nn = $("input[name=goods_name]").val()
            if(nn.length>0 && check1){
                submit_check(true)
            }else{
                submit_check(false)
            }


        }

        function submit_check(boo){


            if(boo){
                $("input[type=submit]").removeAttr("disabled")
                $(".alert-error").hide()
            }else{
                $("input[type=submit]").attr("disabled","disabled")
                $(".alert-error").show()

            }
        }

        function toDecimal2(x) {
            var f = parseFloat(x);
            if (isNaN(f)) {
                return false;
            }
            var f = Math.round(x*100)/100;
            var s = f.toString();
            var rs = s.indexOf('.');
            if (rs < 0) {
                rs = s.length;
                s += '.';
            }
            while (s.length <= rs + 2) {
                s += '0';
            }
            return s;
        }

        function checkPhone(phone){
            if(!(/^1[34578]\d{9}$/.test(phone))){
                return false;
            }
            return true;
        }

    });

//    $('#select').click(function () {
//        $.ajax({
//            type : "POST",  //提交方式
//            url : "<?php //echo urlShop('goods','goods_select') ?>//",//路径
//
//            success : function(result) {//返回数据根据结果进行相应的处理
//                  console.log(result);
//                $("select[name=goods_name_show]").append(result)
//
//            }
//        });
//    });

*/


</script>